<?php
class Login extends CI_Controller {
	public function __construct() {
		parent::__construct();

		$this->load->helper(array('form','url'));
		$this->load->library('form_validation');
	}

	public function index() {
		$this->load->view('pages/user/login_form');
	}

	public function submit() {

		if ($this->_submit_validate() === FALSE) {
			$this->index();
			return;
		}

		//redirect('/');
		//if($this->authenticate()) $this->load->view('pages/home');
		//else $this->load->view('pages/user/login_form');
		redirect('/');

	}

	private function _submit_validate() {

		$this->form_validation->set_rules('account', 'Account',
			'trim|required|callback_authenticate');

		$this->form_validation->set_rules('password', 'Password',
			'trim|required');

		$this->form_validation->set_message('authenticate','登录名或密码错误');

		return $this->form_validation->run();

	}

	public function authenticate() {

		return Current_User::login($this->input->post('account'),
									$this->input->post('password'));

	}
}
